package com.lzxuni.modules.weather.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.HumpUtils;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.weather.entity.WeatherInfo;
import com.lzxuni.modules.weather.mapper.WeatherInfoHourMapper;
import com.lzxuni.modules.weather.service.WeatherInfoHourService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 天气信息_小时天气管理 服务实现类
 * </p>
 *
 * @author LaoNa
 * @since 2020-11-27
 */
@Service
public class WeatherInfoHourServiceImpl extends ServiceImpl<WeatherInfoHourMapper, WeatherInfo> implements WeatherInfoHourService {

    @Override
    public PageInfo<WeatherInfo> queryPage(PageParameter pageParameter, WeatherInfo weatherInfo) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    HumpUtils.humpToLine(pageParameter.getSidx()) + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<WeatherInfo> weatherInfoList = baseMapper.queryList(weatherInfo);
        PageInfo<WeatherInfo> pageInfo = new PageInfo<>(weatherInfoList);
        return pageInfo;
    }

    @Override
    public List<WeatherInfo> queryList(WeatherInfo weatherInfo) {
        List<WeatherInfo> weatherInfoList = baseMapper.queryList(weatherInfo);
        return weatherInfoList;
    }
    @Override
    public Workbook exportExcel(WeatherInfo weatherInfo) throws IOException {
        PageHelper.orderBy("create_date" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        List<WeatherInfo> weatherInfoList =baseMapper.queryList(weatherInfo);
        List<String> list0 = new ArrayList<>();
        list0.add("所属地块");
        list0.add("年份");
        list0.add("时间");
        list0.add("空气温度");
        list0.add("空气湿度");

        list0.add("光照强度");
        list0.add("地温");
        list0.add("风速");
        list0.add("风向");
        list0.add("降雨量");

        list0.add("蒸发量");
        list0.add("情况说明");

        listData1.add(list0);
        if (weatherInfoList != null && weatherInfoList.size() > 0) {
            for (int i = 0; i < weatherInfoList.size(); i++) {
                WeatherInfo weatherInfo1 = weatherInfoList.get(i);
                List<String> list = new ArrayList<>();

                list.add(weatherInfo1.getLandNum());
                list.add(weatherInfo1.getInfoYear());
                list.add(DateFormatUtils.format(weatherInfo1.getCreateDate(),"yyyy-MM-dd hh:mm:ss"));
                list.add(weatherInfo1.getAirHumidity().toString());
                list.add(weatherInfo1.getAirTemperature().toString());

                list.add(weatherInfo1.getLightIntensity().toString());
                list.add(weatherInfo1.getEarthTemperature().toString());
                list.add(weatherInfo1.getWindSpeed().toString());
                list.add(weatherInfo1.getWindDirection().toString());
                list.add(weatherInfo1.getRainfall().toString());

                list.add(weatherInfo1.getEvaporationCapacity().toString());
                list.add(weatherInfo1.getRemarks());

                listData1.add(list);
            }
        }
        dataMap.put("小时天气", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "小时天气");
        return wb;
    }

}
